const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatTime2 = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-');
}

const formatTime3 = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

module.exports = {
  formatTime,
  formatTime2: formatTime2,
  formatTime3: formatTime3
}

// 跳转到指定页面
function navigateTo(url) {
  wx.navigateTo({
    url: url,
    success: function (res) { },
    fail: function (res) { },
    complete: function (res) { },
  })
}

// module.exports.navigateToWeather = navigateToWeather
module.exports.navigateTo = navigateTo

// 判断字符串是否为空
function isNull(str) {
  if (str == undefined) return true;
  if (str == "") return true;
  var regu = "^[ ]+$";
  var re = new RegExp(regu);
  return re.test(str);
}

module.exports.isNull = isNull

// 判断是否登录
function isLogin(token) {
  if (token == undefined || token == null || token.length <= 0 || token.trim.length <= 0) {
    wx.showToast({ title: '请先登录', });
    return false;
  } else {
    return true;
  }
}

module.exports.isLogin = isLogin

// 判断是否登录并跳转
function isToLogin(token) {
  if (token == undefined || token == null || token.length <= 0 || token.trim.length <= 0) {
    wx.navigateTo({
      url: '/pages/sys/login/login',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    return false;
  } else {
    return true;
  }
}

module.exports.isToLogin = isToLogin